

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author andus.top
 * @Date 2021/6/29 14:33
 */
public class ExcelReaderTest {

    public static void main(String[] args) throws IOException, IllegalAccessException {

        ExcelReader excelReader = new ExcelReader("C:\\Users\\YSL\\Desktop\\read-test.xlsx");

        // test1: 获取sheet所有或指定位置的内容
        System.out.println("test1: 获取sheet所有或指定位置的内容");
        //List<List<Object>> sheetContent = excelReader.getSheetContent(0, 5, 11, 1, 2);
        List<List<Object>> sheetContent = excelReader.getSheetContent(0);
        for (List<Object> objects : sheetContent) {
            if(objects != null) {
                for (Object object : objects) {
                    if (object != null) {
                        System.out.print(String.valueOf(object) + ", ");
                    }else{
                        System.out.print("null, ");
                    }
                }
                System.out.println("\n");
            }else{
                System.out.println("跳过空行");
            }
        }

        // test2: 遍历excel每个sheet，获取sheet指定范围的内容
        System.out.println("\ntest2: 遍历excel每个sheet，获取sheet指定范围的内容");
        excelReader.clearCache();
        Map<String, List<List<Object>>> workbookContentMap = excelReader.getWorkbookContentMap(5, 11, 1, 2);
        workbookContentMap.forEach((k, v) -> {
            System.out.println(k);
            v.forEach(i -> {
                if(i != null)
                    i.forEach(j -> {
                        System.out.print(j + ", ");
                    });
                System.out.println();
            });
        });

        // test3: 遍历excel每个sheet，获取sheet指定范围的内容
        System.out.println("\ntest3: 遍历excel每个sheet，获取sheet指定范围的内容");
        excelReader.clearCache();
        List<List<List<Object>>> workbookContentList = excelReader.getWorkbookContentList();
        //List<List<List<Object>>> workbookContentList = excelReader.getWorkbookContentList(5, 11, 1, 2);
        workbookContentList.forEach(sheet -> {
            sheet.forEach(row -> {
                if(row != null) {
                    row.forEach(cell -> {
                        if (cell != null)
                            System.out.print(cell + ", ");
                    });
                    System.out.println();
                }
            });
        });

        // test4: 获取sheet内容，按指定行作为表头，转存为List<Bean> 结构
        System.out.println("\ntest4: 获取sheet内容，按指定行作为表头，转存为List<Bean> 结构");
        List<Bean> beans = excelReader.sheetList2Bean(0, 1, () -> new Bean());
        //List<Bean> beans = excelReader.sheetList2Bean("Sheet1", 1, Bean::new);
        beans.forEach(System.out::println);

        // test5: 获取sheet内容，按指定行作为表头，转存为 Map 结构
        System.out.printf("\ntest5: 获取sheet内容，按指定行作为表头，转存为 Map 结构");
        List<Map<String, Object>> mapList = excelReader.sheetList2Map("Sheet1", 1);
        mapList.forEach(System.out::println);

        excelReader.close();
    }
	
}
